import {Component, ElementRef, Input, OnInit} from '@angular/core';
import {FormInfoModel} from './form-info.model';

@Component({
  selector: 'app-form-info',
  templateUrl: './form-info.component.html',
  styleUrls: ['./form-info.component.scss']
})
export class FormInfoComponent implements OnInit {

  @Input() public formInfoModel: FormInfoModel;
  public expandedKeys = [];

  constructor(private el: ElementRef) {
  }

  ngOnInit() {

  }

  /**
   * 处理选中
   */
  isNotSelected(data, value: string): boolean {
    return data ? data.indexOf(value) === -1 : true;
  }

  /**
   * 获取对象key集合
   */
  public getObjKeys(data) {
    return Object.getOwnPropertyNames(data);
  }

  /**
   * 获取焦点
   */
  public focus(id) {
    const dom = this.el.nativeElement.querySelector('#' + id);
    if (dom) {
      dom.focus();
    }
  }

  public openChange(parentKey, trees) {
    if (parentKey) {
      this.expandedKeys = this.getParentKeys(parentKey, trees);
    }
  }


  /**
   * 获取所有的父级id
   */
  public getParentKeys(parentKey, trees) {
    let array = [];
    for (const tree of  trees) {
      if (tree.key === parentKey) {
        return [tree.key];
      }
      if (tree.children) {
        array = array.concat(this.getParentKeys(parentKey, tree.children));
        if (array.length > 0) {
          array.push(tree.key);
          return array;
        }
      }
    }
    return array;
  }
}
